import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_swiper_plus/flutter_swiper_plus.dart';
import 'package:paly_android/base/base_widget.dart';
import 'package:paly_android/bean/article_data_entity.dart';
import 'package:paly_android/bean/banner_entity.dart';
import 'package:paly_android/bloc/article_bloc.dart';
import 'package:paly_android/bloc/banner_bloc.dart';
import 'package:paly_android/route/routes.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeSliverPage extends BaseWidget {
  @override
  BaseWidgetState<BaseWidget> getState() => _HomePageSliverState();
}

class _HomePageSliverState extends BaseWidgetState<HomeSliverPage> {
  int _page = 0;

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  bool refresh = true;

  void _onRefreshList() {
    _page = 0;
    getArticles();
  }

  void _loadMore() {
    _page++;
    getArticles(isRefresh: false);
  }

  late ArticleBloc bloc;
  late BannerBloc bannerBloc;
  int _bannerIndex = 0;

  @override
  void didChangeDependencies() {
    bloc = ArticleBloc();
    bannerBloc = BannerBloc();
    _onRefreshList();
    bannerBloc.getBanner();

    super.didChangeDependencies();
  }

  void getArticles({bool isRefresh: true}) {
    refresh = isRefresh;
    bloc.getArticles(isRefresh, _page);
  }

  Widget getArticleWidget(List<ArticleDataDatas> data) {
    int len = data.length;
    List<Widget> widgets = data.asMap().entries.map((e) {
      int index = e.key;
      var bean = e.value;
      return InkWell(
        onTap: () {
          Navigator.of(context).pushNamed(MyRouter.WEB,
              arguments: {"title": bean.title, "url": bean.link});
        },
        child: Column(
          children: [
            ListTile(
              title: Text(bean.title, overflow: TextOverflow.ellipsis),
              subtitle: Text(
                bean.desc,
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            if (index < len - 1) Divider()
          ],
        ),
      );
    }).toList();

    return Column(
      children: widgets,
    );
  }

  @override
  Widget baseBuild(BuildContext context) {
    return MultiBlocProvider(
        providers: [
          BlocProvider<ArticleBloc>(create: (context) => bloc),
          BlocProvider<BannerBloc>(create: (context) => bannerBloc),
        ],
        child: SmartRefresher(
          enablePullUp: true,
          controller: _refreshController,
          onRefresh: _onRefreshList,
          onLoading: _loadMore,
          child: CustomScrollView(
            slivers: [
              SliverToBoxAdapter(
                child: BlocBuilder<BannerBloc, List<BannerEntity>>(
                  builder: (BuildContext context, state) {
                    LogUtil.d("home page BlocBuilder  builder ");
                    return Container(
                        height: 200,
                        child: Padding(
                          padding: EdgeInsets.only(bottom: 10),
                          child: Swiper(
                            itemBuilder: (BuildContext context, int index) {
                              return Image.network(state[index].imagePath!,
                                  fit: BoxFit.fill);
                            },
                            itemCount: state.length,
                            index: _bannerIndex,
                            onIndexChanged: (index) {
                              _bannerIndex = index;
                            },
                            pagination: SwiperPagination(),
                            // control: SwiperControl(),
                          ),
                        ));
                  },
                ),
              ),
              SliverList(
                delegate: SliverChildListDelegate([
                  BlocBuilder<ArticleBloc, ArticleDataEntity>(
                    buildWhen: (pre, cur) {
                      if (refresh) {
                        _refreshController.refreshCompleted();
                      } else {
                        _refreshController.loadComplete();
                      }
                      return true;
                    },
                    builder: (BuildContext context, state) {
                      LogUtil.e(" article builder");
                      return getArticleWidget(state.datas);
                    },
                  )
                ]),
              )
            ],
          ),
        ));
  }

  @override
  String getAppBarTitle() => "首页";
}
